if(LLVM_BYE_LINK_INTO_TOOLS)
  message(WARNING "Setting LLVM_BYE_LINK_INTO_TOOLS=ON only makes sense for testing purpose")
endif()

set(LLVM_DIR /Users/aserlili/Documents/Codes/llvm-build/build-19.1.3/Clion/Debug/lib/cmake/llvm/)

find_package(LLVM REQUIRED CONFIG)

list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(AddLLVM)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
message(STATUS "LLVM_DEFINITIONS_LIST: ${LLVM_DEFINITIONS_LIST}")

add_definitions(${LLVM_DEFINITIONS_LIST})
include_directories(${LLVM_INCLUDE_DIRS})

# 设置 C++ 标准版本
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 替换原有的 --libs 选项
execute_process(
    COMMAND ${LLVM_CONFIG} --libs Core --link-shared  # 添加 --link-shared
    OUTPUT_VARIABLE MY_LLVM_LIBS
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

message(STATUS "MY_LLVM_LIBS Before: ${MY_LLVM_LIBS}")
separate_arguments(MY_LLVM_LIBS UNIX_COMMAND "${MY_LLVM_LIBS}")
message(STATUS "MY_LLVM_LIBS After: ${MY_LLVM_LIBS}")

# The plugin expects to not link against the Support and Core libraries,
# but expects them to exist in the process loading the plugin. This doesn't
# work with DLLs on Windows (where a shared library can't have undefined
# references), so just skip this example on Windows.
if (NOT WIN32 AND NOT CYGWIN)
  add_llvm_pass_plugin(CustomOllvm
      CustomOllvm.cpp
    DEPENDS
    intrinsics_gen
    BUILDTREE_ONLY
   )

  #  target_link_libraries(CustomOllvm PRIVATE ${MY_LLVM_LIBS})
  target_link_libraries(CustomOllvm PRIVATE ${MY_LLVM_LIBS}  "-Wl,-undefined,dynamic_lookup")

  # 关闭RTTI以匹配LLVM编译选项
  target_compile_options(CustomOllvm PRIVATE -fno-rtti)

  # install(TARGETS ${name} RUNTIME DESTINATION "${LLVM_EXAMPLES_INSTALL_DIR}")
  # set_target_properties(${name} PROPERTIES FOLDER "Examples")
endif()

